//
//  DanmuModel.swift
//  danmuDemo
//
//  Created by BigL055 on 16/6/16.
//  Copyright © 2016年 bigL.MoreDemo.com. All rights reserved.
//

import UIKit
import YYText


enum BLCellType : String {
    /// 弹幕消息
    case CellChatMessage       = "chatmsg"
    /// 领取在线鱼丸暴击消息
    case CellOnlineGift        = "onlinegift"
    /// 赠送礼物消息
    case CellGiftGivingNotice  = "dgb"
    /// 用户进房通知消息
    case CellUserEnter         = "uenter"
    /// 管理员封禁用户发言
    case CellBanUser           = "blackres"
    
    /// 保持连接
    case CellKeeplive          = "keeplive"
    /// 用户升级
    case CellUpGrade           = "upgrade"
}

class BLDanmuModel: NSObject {
    
    var type : BLCellType?
    
    var giftName : String? = ""

    //图标解析器
    let emoteParser        = YYTextSimpleEmoticonParser()
    
    
    
    
    
    
    // MARK: - CellNewGiftType
    var roomID : String?     //房间 id
    var customType : String? //客户端类型:默认值 0(表示 web 用户)
    var groupID : String?    //弹幕组 id
    var UID : String?        //发送者 uid
    var nickName : String?   //发送者昵称
    var text : String?       //弹幕文本内容
    var CID : String?        //弹幕唯一 ID
    var level : String         = "1"//用户等级
    
    
    // MARK: - CellNewGiftType
    var giftID : String?      //礼物 id
    var giftStyle : String?   //礼物显示样式
    var douyuWeight : String? //主播体重
    var hitCount : String?    //礼物连击次数:默认值 1(表示 1 连击)
    
}


extension BLDanmuModel{
    
    
    func setModelFromStirng(dataStr : String) {
        
        
        roomID = bl_regexWithPattern("(?<=rid@=).*?(?=/)", inputStr: dataStr).first ?? ""
        UID    = bl_regexWithPattern("(?<=uid@=).*?(?=/)", inputStr: dataStr).first ?? ""
        nickName = bl_regexWithPattern("(?<=nn@=).*?(?=/)", inputStr: dataStr).first ?? ""

        
        
        switch type! {
        case .CellChatMessage:
            setChatMessage(dataStr)
        case .CellGiftGivingNotice:
            setGiftGivingNotice(dataStr)
        case .CellUserEnter:
            setUserEnter(dataStr)
        case .CellBanUser:
            setBanUser(dataStr)
        case .CellUpGrade:
            setUpGrade(dataStr)
        default: break
            
            
        }
    }
    
}




// MARK: - 不同类型的属性设置
extension BLDanmuModel{
    
    /// MARK: - 弹幕消息
    private func setChatMessage(dataStr : String) {
        /*
         type@=chatmsg/
         rid@=335166/
         uid@=44089628/
         nn@=aydxj666/
         txt@=真的/
         cid@=3274c7eff62d45f50724000000000000/
         ic@=avatar@Sdefault@S13/
         level@=1/
         egtt@=0/
         */
        
        groupID    = bl_regexWithPattern("(?<=gid@=).*?(?=/)", inputStr: dataStr).first ?? ""
        text       = bl_regexWithPattern("(?<=txt@=).*?(?=/)", inputStr: dataStr).first ?? ""
        CID        = bl_regexWithPattern("(?<=cid@=).*?(?=/)", inputStr: dataStr).first ?? ""
        customType = bl_regexWithPattern("(?<=ct@=).*?(?=/)", inputStr: dataStr).first ?? ""
        level      = bl_regexWithPattern("(?<=level@=).*?(?=/)", inputStr: dataStr).first ?? "1"
        
        //表情字符串数组
        let emotStrArray = bl_regexWithPattern("\\[emot:(\\w+\\d+)\\]", inputStr: text!)

        // 表情解析器
        var mapper       = [String : UIImage]()

        // 表情图标
        for emoteStr in emotStrArray {
            //表情示例 : txt@=[emot:dy101]我来也
            var emoteImageName = emoteStr.stringByReplacingOccurrencesOfString("[emot:", withString: "")
            emoteImageName     = emoteImageName.stringByReplacingOccurrencesOfString("]", withString: "")
            mapper[emoteStr]   = UIImage(named: emoteImageName)
        }
        
        emoteParser.emoticonMapper = mapper
        
    }
    
    /// MARK: - 赠送礼物消息
    private func setGiftGivingNotice(dataStr : String) {
        /*
         type@=dgb/
         rid@=335166/
         gfid@=192/
         gs@=2/
         uid@=46742449/
         nn@=程晓伟看直播/
         ic@=avatar@Sface@S201606@S16@S47bbc93785a96749d9071e895ff4b335/
         level@=6/
         dw@=102495934/
         hits@=50/
         ct@=0/
         cm@=326247/
         */
        
        giftID      = bl_regexWithPattern("(?<=gfid@=).*?(?=/)", inputStr: dataStr).first ?? ""
        giftStyle   = bl_regexWithPattern("(?<=gs@=).*?(?=/)", inputStr: dataStr).first ?? ""
        level       = bl_regexWithPattern("(?<=level@=).*?(?=/)", inputStr: dataStr).first ?? ""
        douyuWeight = bl_regexWithPattern("(?<=dw@=).*?(?=/)", inputStr: dataStr).first ?? ""
        hitCount    = bl_regexWithPattern("(?<=hits@=).*?(?=/)", inputStr: dataStr).first ?? "1"
        customType  = bl_regexWithPattern("(?<=ct@=).*?(?=/)", inputStr: dataStr).first ?? ""
        
        // 礼物解析器
        var mapper = [String : UIImage]()

        giftID = "gfid@=" + giftID!

        //  鱼丸图标
        mapper[giftID!] = UIImage(named: "gift_yuWan_56x56_")

        emoteParser.emoticonMapper = mapper
        
    }
    
    /// MARK: - 用户进房通知消息
    func setUserEnter(dataStr : String) {
        /*
         type@=uenter/rid@=335166/uid@=12238867/nn@=惜浅笑c/level@=21ic@=avatar@S012@S23@S88@S67_avatar/egtt@=0/
         */
        level    = bl_regexWithPattern("(?<=level@=).*?(?=/)", inputStr: dataStr).first ?? ""
        text     = nickName! + " 进入了直播间"
        
    }
    
    /// 管理员封禁用户发言
    func setBanUser(dataStr : String) {
       /// type@=newblackres/ret@=0/rid@=56040/gid@=-9999/otype@=1/sid@=11002474/did@=50486421/snic@=a456333/dnic@=萌筱静/endtime@=1466477301
        nickName = bl_regexWithPattern("(?<=dnic@=).*?(?=/)", inputStr: dataStr).first ?? ""

    }
    
    
    func setUpGrade(dataStr : String) {
        /// type@=upgrade/uid@=21477996/rid@=4809/gid@=-9999/nn@=Angle秋波/level@=4/
        level = bl_regexWithPattern("(?<=level@=).*?(?=/)", inputStr: dataStr).first ?? ""
    }
}
